package com.yourcompany;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

import java.net.InetSocketAddress;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.yourcompany.handle.AdminHandle;
import com.yourcompany.handle.RevicMessageHandle;

/**
 * 服务类
 * @author tzj
 * 
 */
public class PollServer {

	Logger logger=Logger.getLogger("server");
	/**
	 * 数据库操作线
	 */
	ExecutorService dboptGroup = Executors.newFixedThreadPool(8);

	private int sobacklog;

	private int port;

	private int bossthreads;

	private int dbthreads;

	private int workthreads;

	private int readtimeout;
	
	/**
	 * 实际处理类
	 */
	@Autowired
	RevicMessageHandle messageHandle;
	
	@Autowired
	AdminHandle adminHandle;
	
	BlockingQueue<Channel> channels = new LinkedBlockingQueue<Channel>();

	public void start() throws InterruptedException {

		
		ServerBootstrap bootstrap = new ServerBootstrap();// 启动辅助类
		
		// io线程和工作线
		NioEventLoopGroup group = new NioEventLoopGroup(bossthreads);
		

		NioEventLoopGroup workGroup = new NioEventLoopGroup(workthreads);
		
	
		bootstrap.group(group, workGroup);
		bootstrap.channel(NioServerSocketChannel.class);//
		bootstrap.localAddress(new InetSocketAddress(port));// 绑定端口
		bootstrap.option(ChannelOption.SO_BACKLOG, sobacklog);
		bootstrap.option(ChannelOption.ALLOCATOR,PooledByteBufAllocator.DEFAULT);
		
		try {
			bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {//
						protected void initChannel(SocketChannel ch)
								throws Exception {
							ch.pipeline()
							.addLast(new LoggingHandler(LogLevel.INFO))
									.addLast(messageHandle)
									.addLast(adminHandle);
						}
						

					});
			ChannelFuture f = bootstrap.bind().sync();// 实际绑定操作
			
			
			logger.debug(PollServer.class.getName() + " 服务器启动完成"
					+ f.channel().localAddress());

			f.channel().closeFuture().sync();// ;// 等待 服务器关
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			group.shutdownGracefully().sync();// 优雅关闭
			workGroup.shutdownGracefully().sync();
		}
	}

	public int getSobacklog() {
		return sobacklog;
	}

	public void setSobacklog(int sobacklog) {
		this.sobacklog = sobacklog;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public int getBossthreads() {
		return bossthreads;
	}

	public void setBossthreads(int bossthreads) {
		this.bossthreads = bossthreads;
	}

	public int getDbthreads() {
		return dbthreads;
	}

	public void setDbthreads(int dbthreads) {
		this.dbthreads = dbthreads;
		dboptGroup=Executors.newFixedThreadPool(dbthreads);
	}

	public int getWorkthreads() {
		return workthreads;
	}

	public void setWorkthreads(int workthreads) {
		this.workthreads = workthreads;
	}

	public int getReadtimeout() {
		return readtimeout;
	}

	public void setReadtimeout(int readtimeout) {
		this.readtimeout = readtimeout;
	}

	public ExecutorService getDboptGroup() {
		return dboptGroup;
	}

	public AdminHandle getAdminHandle() {
		return adminHandle;
	}

	public void setAdminHandle(AdminHandle adminHandle) {
		this.adminHandle = adminHandle;
	}

	public BlockingQueue<Channel> getChannels() {
		return channels;
	}

	public void setChannels(BlockingQueue<Channel> channels) {
		this.channels = channels;
	}

	public void setDboptGroup(ExecutorService dboptGroup) {
		this.dboptGroup = dboptGroup;
	}

	
	
}